/**
 * module index
 */
(function() {
	'use strict';
	
	require.config({
		baseUrl: "/miwawa/resources/lib",
		paths: {
			"jquery.chart.sparkline": "miwawa/1.0.2/js/core/module/chart/sparkline",
			"jquery.chart.easypiechart": "miwawa/1.0.2/js/core/module/chart/easypiechart"
		}
	});
	
	define(['moment','jquery.chart.sparkline','jquery.chart.easypiechart','jquery.bootstrap.table'],function(moment, sparkline, easypiechart, table){
		window["getRangeMonth"] = function(){
			//console.log("call this method : getRangeMonth");
			var min = moment().startOf('month').format("YYYY-MM-DD"); 
			var max = moment().endOf('month').format("YYYY-MM-DD"); 
			return JSON.stringify([min, max]);
		};
		
		window["createData"] = {
			"schedule":function(data){
				jQuery.each(data,function(inx, row){
					jQuery("#classes-query").append('<li><a href="javascript:void(0);" onclick="handlerQuery(this);">'+row["课程"]+'</a></li>');
				});
				jQuery("#table-schedule").bootstrapTable("removeAll").bootstrapTable("load",data);
			},
			"checkrate":function(data){
				jQuery.each(data,function(i, o){
					if(o.name == "本月到课率"){
						jQuery("[data-epc-percent='"+o.name+"']").text(o.percent+"%").parent().attr("data-percent", o.percent);
						jQuery("[data-epc-count='"+o.name+"']").text(o.count);
					}
				});
			},
			"calloncount":function(data){
				jQuery("[data-epc-percent='"+data[0].name+"']").text(data[0].percent+"%").parent().attr("data-percent", data[0].percent);
				jQuery("[data-epc-count='"+data[0].name+"']").text(data[0].count);
				easypiechart(".easypiechart");
			},
			"salescount":function(data){
				jQuery.each(data,function(i, o){
					jQuery("[data-epc-percent='"+o.name+"']").text(o.percent+"%").parent().attr("data-percent", o.percent);
					jQuery("[data-epc-count='"+o.name+"']").text(o.count);
				});
				
				easypiechart(".easypiechart");
			},
			"salesranking":function(data){
				require(['text!/miwawa/content/portal/template.html'+"?"+Date.parse(new Date()), 'handlebars'],function(html, Handlebars){
					var content = jQuery("<div>").html(html).find("#salesranking-template").html();

					jQuery(".ranking-list").empty().append(function(){
						var compile = Handlebars.compile(content);

						Handlebars.registerHelper("ranking", function(index, flag){
							return (index == 0 && flag == "top") ? flag : ++index;
						});
						
						return compile(data);
					});
				});
			},
			"salescustomer":function(data){
				jQuery("#table-salescustomer").bootstrapTable("removeAll").bootstrapTable("load",data);
			},
			"bespokecustomer":function(data){
				jQuery("#table-bespokecustomer").bootstrapTable("removeAll").bootstrapTable("load",data);
			},
			"consuming":function(data){
				jQuery("#table-amount").bootstrapTable("removeAll").bootstrapTable("load",data);
			},
			"yearofsales":function(data){
				jQuery.each(data,function(i, o){
					var array = jQuery.utils.toArray(o.count);
					
					jQuery("[data-year-count='"+o.name+"']").text(jQuery.utils.toTotal(array));
					jQuery("[data-year-counts='"+o.name+"']").attr("data-values","["+o.count+"]");
					jQuery("[data-month-count='"+o.name+"']").text(array[array.length-1]);
				});
				sparkline(".sparkline");
			}
		};
		
		return {
			adviser:function(){
				jQuery.ajaxsend({
					url: "/miwawa/{dbname}/dashboard/adviser".replace("{dbname}", dbname),
					success: function(data){
						if(data){
							table();
							createData["salescount"](data["salescount"]);
							createData["salesranking"](data["salesranking"]);
							createData["bespokecustomer"](data["bespokecustomer"]);
							jQuery(".loading-container").hide();
						}
					}
				});
			},
			teacher:function(){
				require(['moment'], function(moment){
					jQuery.ajaxsend({
						url: "/miwawa/{dbname}/dashboard/teacher".replace("{dbname}", dbname),
						data:{
							"params":{
								"fromDate":moment().startOf('month').format("YYYY-MM-DD"),
								"overDate":moment().endOf('month').format("YYYY-MM-DD")
							}
						},
						success:function(data){
							if(data){
								table();
								createData["schedule"](data["schedule"]);
								createData["checkrate"](data["checkrate"]);
								createData["calloncount"](data["calloncount"]);
							}
						}
					});
				});
			},
			leaders:function(){
				jQuery.ajaxsend({
					url:"/miwawa/{dbname}/dashboard/adviser".replace("{dbname}",dbname),
					success:function(data){
						if(data){
							createData["salescount"](data["salescount"]);
							createData["salesranking"](data["salesranking"]);
						}
					}
				});
				
				jQuery.ajaxsend({
					url:"/miwawa/{dbname}/dashboard/leaders".replace("{dbname}",dbname),
					success:function(data){
						if(data){
							table();
							createData["consuming"](data["consuming"]);
							createData["yearofsales"](data["yearofsales"]);
							jQuery(".loading-container").hide();
						}
					}
				});
			}
		};
	});
	
})(this);